import { defineStore } from 'pinia';

export const screenData = defineStore('screenData', {
  state: () => ({
    componentList: [] as any[],
    layerList: [] as any[],
    recordList: [] as any[],
    recordList4ctrlY: [] as any[],
  }),
  actions: {
    undateAddComponentList(object: any) {
      this.componentList.push(object);
    },
    undateAddComponentList1(array: any) {
      this.componentList = array;
    },
    undateAddLayerList(object: any) {
      this.layerList.unshift(object);
    },
    undateAddLayerList1(array: any) {
      this.layerList = array;
    },
    undateRemoveLayerList(i: number) {
      this.layerList = this.layerList.filter((f) => f.id !== i);
    },
    undateRemoveLayerList1() {
      this.layerList.unshift();
    },
    updateLayerList(id: number, num: number) {
      switch (num) {
      case 1:
        let d = 0;
        for (let i = 0; i < this.layerList.length; i++) {
          if (this.layerList[i].id === id) {
            if (i === 0) {
              return;
            }
            d = this.layerList[i].zIndex;
            this.layerList[i].zIndex = this.layerList[i - 1].zIndex;
            this.layerList[i - 1].zIndex = d;
          }
        }
        let a = 0;
        for (let i = 0; i < this.componentList.length; i++) {
          if (this.componentList[i].id === id) {
            a = this.componentList[i].zIndex;
            this.componentList[i].zIndex = this.componentList[i + 1].zIndex;
            this.componentList[i + 1].zIndex = a;
          }
        }
        this.componentList.sort((a, b) => a.zIndex - b.zIndex);
        break;
      case 2:
        let f = 0;
        for (let i = 0; i < this.layerList.length; i++) {
          if (this.layerList[i].id === id) {
            if (i === this.layerList.length - 1) {
              return;
            }
            f = this.layerList[i].zIndex;
            this.layerList[i].zIndex = this.layerList[i + 1].zIndex;
            this.layerList[i + 1].zIndex = f;
          }
        }
        let c = 0;
        for (let i = 0; i < this.componentList.length; i++) {
          if (this.componentList[i].id === id) {
            c = this.componentList[i].zIndex;
            this.componentList[i].zIndex = this.componentList[i - 1].zIndex;
            this.componentList[i - 1].zIndex = c;
          }
        }
        this.componentList.sort((a, b) => a.zIndex - b.zIndex);
        break;
      case 3:
        let h = 0;
        for (let i = 0; i < this.layerList.length; i++) {
          h = this.layerList[0].zIndex + 1;
          if (this.layerList[i].id === id) {
            this.layerList[i].zIndex = h;
            break;
          }
          this.layerList[i].zIndex -= 1;
        }
        let l = 0;
        for (let i = 0; i < this.componentList.length; i++) {
          l = this.componentList[this.componentList.length - 1].zIndex + 1;
          if (this.componentList[i].id === id) {
            this.componentList[i].zIndex = l;
            break;
          }
          this.componentList[i].zIndex -= 1;
        }
        this.componentList.sort((a, b) => a.zIndex - b.zIndex);
        break;
      case 4:
        let u = 0;
        for (let i = this.layerList.length - 1; i > -1; i--) {
          u = this.layerList[this.layerList.length - 1].zIndex - 1;
          if (this.layerList[i].id === id) {
            this.layerList[i].zIndex = u;
            break;
          }
          this.layerList[i].zIndex += 1;
        }
        let r = 0;
        for (let i = 0; i < this.componentList.length; i++) {
          r = this.componentList[0].zIndex - 1;
          if (this.componentList[i].id === id) {
            this.componentList[i].zIndex = r;
            break;
          }
          this.componentList[i].zIndex += 1;
        }
        this.componentList.sort((a, b) => a.zIndex - b.zIndex);
        break;
      default:
        break;
      }
    },
    undateAddRecordList(array: any) {
      this.recordList.push(array);
    },
    undateRemoveRecordList() {
      this.recordList4ctrlY.push(this.recordList.pop());
    },
    undateRemoveRecordList1() {
      this.layerList.shift();
    },
    undateRemoveRecordList4ctrlY() {
      this.recordList4ctrlY.pop();
    },
  },
});
